package com.eclink.asset.hardware.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eclink.asset.hardware.domain.dao.Device;
import com.eclink.asset.hardware.domain.dao.SysUserDevice;
import com.eclink.asset.hardware.mapper.DeviceMapper;
import com.eclink.asset.hardware.mapper.SysUserDeviceMapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * 公司Service
 *
 * @author chenshixian
 */
@Service
public class DeviceService {

    @Resource
    private DeviceMapper mapper;

    @Resource
    private SysUserDeviceMapper sysUserDeviceMapper;

    @Resource
    private RfidService rfidService;

    /**
     * 查询
     *
     * @param id ID
     * @return 结果
     */
    public Device selectById(Long id) {
        return mapper.selectById(id);
    }

    /**
     * 查询名称
     *
     * @param id ID
     * @return 名称
     */
    public String getNameById(Long id) {
        Device entity = selectById(id);
        if (entity == null) {
            return null;
        }
        return entity.getName();
    }

    /**
     * 查询
     *
     * @return 列表
     */
    public List<Device> selectList() {
        QueryWrapper<Device> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        qw.eq("user_id", SecurityUtils.getUserId());
        return mapper.selectList(qw);
    }

    /**
     * 查询
     *
     * @return 列表
     */
    public List<Device> selectMyList() {

        MPJLambdaWrapper<Device> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(Device.class);
        wrapper.innerJoin(SysUserDevice.class, SysUserDevice::getDeviceId, Device::getId);
        wrapper.eq(SysUserDevice::getUserId, SecurityUtils.getUserId());
        return mapper.selectJoinList(Device.class, wrapper);


    }

    public void deleteMyDevice() {
        QueryWrapper<SysUserDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", SecurityUtils.getUserId());
        sysUserDeviceMapper.delete(wrapper);
    }

    public void deleteMyDevice(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return;
        }
        List<Long> params = new ArrayList<>();
        Collections.addAll(params, ids);
        QueryWrapper<SysUserDevice> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", SecurityUtils.getUserId());
        wrapper.in("device_id", params);
        sysUserDeviceMapper.delete(wrapper);
    }

    /**
     * 查询
     *
     * @return 列表
     */
    public void updateMyDevice(Long[] ids) {

        deleteMyDevice();
        Long userId = SecurityUtils.getUserId();

        List<SysUserDevice> list = new ArrayList<>();
        for (Long id : ids) {
            SysUserDevice sysUserDevice = new SysUserDevice();
            sysUserDevice.setDeviceId(id);
            sysUserDevice.setUserId(userId);
            list.add(sysUserDevice);
        }
        sysUserDeviceMapper.insertBatch(list);

    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<Device> selectListAll() {

        QueryWrapper<Device> qw = new QueryWrapper<>();
        qw.eq("del_flag",0);
        qw.eq("user_id", SecurityUtils.getUserId());
        return mapper.selectList(qw);
    }

    /**
     * 查询所有
     *
     * @return 仓库
     */
    public List<Device> selectListAllAutoStart() {

        LambdaQueryWrapper<Device> qw = new LambdaQueryWrapper<>();
        qw.eq(Device::getDelFlag,0);
        qw.eq(Device::getAutoStartFlag,1);
        return mapper.selectList(qw);
    }

    /**
     * 新增
     *
     * @param entity 仓库
     * @return 结果
     */
    public int insert(Device entity) {
        entity.setDelFlag(0);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUserId(SecurityUtils.getUserId());
        return mapper.insert(entity);
    }

    /**
     * 关闭
     *
     * @param id ID
     * @return 结果
     */
    public void start(Long id) {
        rfidService.start(id);
    }

    /**
     * 关闭
     *
     * @param id ID
     * @return 结果
     */
    public void shutdown(Long id) {
        rfidService.shutdown(id);
    }

    /**
     * 修改仓库
     *
     * @param entity 实体
     * @return 结果
     */
    public int update(Device entity) {
        return mapper.updateById(entity);
    }


    /**
     * 删除仓库信息
     *
     * @param id 仓库主键
     * @return 结果
     */
    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    public void deleteByIds(Long[] ids) {
        for(Long id : ids) {
            deleteById(id);
        }
    }

    /**
     * 根据主键集合查询仓库列表
     *
     * @param ids 主键集合
     * @return 仓库列表
     */
    public List<Device> selectByIdIn(Collection<Long> ids) {
        // 如果主键集合为空，直接返回空集合
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        QueryWrapper<Device> qw = new QueryWrapper<>();
        qw.in("id",ids);
        return mapper.selectList(qw);
    }
}
